home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / misc / bfd / datatest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-17  |  8.6 KB  |  316 lines

  1. /*
  2.  * This will simply give information on the ELF bin, and with variables
  3.  * given as arguments to the program (assuming they are located in data
  4.  * segment), give the offsets between the variables (useful in exploiting
  5.  * overflows in data segment).  Warning: this can give false info
  6.  *
  7.  * To compile: gcc -o datatest datatest.c -lbfd -liberty
  8.  * -- Matt Conover (Shok)
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdarg.h>
  16. #include <errno.h>
  17. #include <ctype.h>
  18. #include <bfd.h>
  19.  
  20. #define ERROR -1
  21.  
  22. /* default data types needed */
  23. bfd *abfd;
  24. bfd_format bfdformat; /* contains format */
  25. enum bfd_flavour bfdflavour; /* contains the file flavour */
  26.  
  27. asection *bssSect, *heapSect, *pltSect;
  28.  
  29.  
  30. void setup(); /* does the main bfd setup */
  31. void showStats(); /* show stats on file */
  32.  
  33. void dumpsects(asection *sect);
  34. void getSyms(char *sym1, char *sym2);
  35.  
  36. void bfderror(char *fmt, ...);
  37.  
  38. int main(int argc, char **argv)
  39. {
  40.    if (argc <= 1)
  41.    {
  42.       fprintf(stderr, "Usage: %s <filename> <var1> <var2>\n\n", argv[0]);
  43.       fprintf(stderr, "Where var1/var2 are variables in the program\n");
  44.       fprintf(stderr, "E.g.: %s ./%s main abfd\n", argv[0], argv[0]);
  45.  
  46.       exit(ERROR); 
  47.    }
  48.  
  49.    bfd_init();
  50.    bfd_set_error_handler((bfd_error_handler_type)bfderror);
  51.  
  52.    abfd = bfd_openr(argv[1], NULL);
  53.  
  54.    if (abfd == NULL)
  55.    {
  56.       bfderror("[bfd_openr] error with opening %s", argv[1]);
  57.       exit(ERROR);
  58.    }
  59.  
  60.    setup(), showStats();
  61.  
  62.    printf("\nHit enter to continue..."), (void)getchar();
  63.  
  64.    dumpsects(bfd_get_section_by_name(abfd, ".text"));
  65.    dumpsects(bfd_get_section_by_name(abfd, ".plt"));
  66.  
  67.    dumpsects(bfd_get_section_by_name(abfd, ".data"));
  68.    dumpsects(bfd_get_section_by_name(abfd, ".bss"));
  69.  
  70.    getSyms(argv[2], argv[3]);
  71.  
  72.    bfd_close(abfd);
  73.    return 0;
  74. }
  75.  
  76. /* ------------------------------------- */
  77.  
  78. void setup()
  79. {
  80.    bfdformat = bfd_object;
  81.    if (bfd_check_format(abfd, bfdformat) == false)
  82.    {
  83.       (void)fprintf(stderr, "error: only object files are supported\n\n");
  84.       exit(ERROR);
  85.    }
  86.  
  87.    bfdflavour = bfd_get_flavour(abfd);
  88.    if (bfdflavour == ERROR)
  89.    {
  90.       bfderror("[bfd_get_flavour] error");
  91.       exit(ERROR);
  92.    }
  93.  
  94.    /* ---------------------------------------------------- */
  95.  
  96.    if ((bfdflavour == bfd_target_unknown_flavour) ||
  97.        ((bfdflavour != bfd_target_aout_flavour) &&
  98.         (bfdflavour != bfd_target_coff_flavour) &&
  99.         (bfdflavour != bfd_target_elf_flavour) &&
  100.         (bfdflavour != bfd_target_msdos_flavour)))
  101.    {
  102.       (void)fprintf(stderr, "Only current supported formats (flavours): "
  103.                             "ELF, COFF, AOUT, and MS-DOS\n");
  104.  
  105.       exit(ERROR);
  106.    }
  107. }
  108.  
  109. /* ------------------------------------- */
  110.  
  111. void showStats()
  112. {   
  113.    (void)printf("Filename: %s\n\n", bfd_get_filename(abfd));
  114.    (void)printf("File's target: %s\n", bfd_get_target(abfd));
  115.  
  116.    (void)printf("File's endianess: ");
  117.  
  118.    if (bfd_little_endian(abfd)) (void)printf("little endian\n");
  119.    else (void)printf("big endian\n");
  120.    
  121.    (void)printf("Bits per byte on file's arch: %u bits\n",
  122.                 bfd_arch_bits_per_byte(abfd));
  123.  
  124.    (void)printf("Bits per address on file's arch: %u bits\n\n",
  125.                 bfd_arch_bits_per_address(abfd));
  126.  
  127.    (void)printf("Start address: %p\n", 
  128.                 (void *)bfd_get_start_address(abfd));
  129.  
  130. }
  131.  
  132. /* ------------------------------------- */
  133.  
  134. /* Dump all the executable's section (independent of type) */
  135. void dumpsects(asection *argsect)
  136. {
  137.    if (argsect == NULL) 
  138.    {
  139.       fprintf(stderr, "\nargsect = NULL... returning\n");
  140.       return;
  141.    }
  142.  
  143.    printf("\n-------------------------------------------------\n\n");
  144.    printf("Section: %s\n", argsect->name);
  145.  
  146.    if (((u_long)argsect->vma - (u_long)argsect->lma) != 0)
  147.      printf("Section Virtual Memory Address (VMA): %p\n",
  148.             (u_long *)argsect->vma);
  149.  
  150.    printf("Section Load Memory Address (LMA): %p\n", 
  151.           (u_long *)argsect->lma);
  152.  
  153.    /* ----------------------------------------- */
  154.  
  155.    printf("Important section flags: ");
  156.  
  157.    if (argsect->flags & SEC_ALLOC) printf("ALLOC ");
  158.    if (argsect->flags & SEC_LOAD) printf("LOAD ");
  159.    if (argsect->flags & SEC_READONLY) printf("READONLY ");
  160.    if (argsect->flags & SEC_CODE) printf("CODE ");
  161.    if (argsect->flags & SEC_DATA) printf("DATA ");
  162.  
  163.    putchar('\n');
  164. }
  165.  
  166. /* ------------------------------------- */
  167.  
  168. void getSyms(char *sym1, char *sym2)
  169. {
  170.    int sym1addr = 0, sym2addr = 0;
  171.    char foundsym1 = 0, foundsym2 = 0;
  172.  
  173.    asymbol **symtab;
  174.    long i, storage, numSyms;
  175.  
  176.    if ((sym1 == NULL) || (sym2 == NULL))
  177.    {
  178.       fprintf(stderr, "\nsym1 or sym2 == NULL.. returning\n");
  179.       return;
  180.    }
  181.  
  182.    storage = bfd_get_symtab_upper_bound(abfd);
  183.    if (storage == ERROR)
  184.    {
  185.       bfderror("[bfd_get_symtab_upper_bound] error");
  186.       exit(ERROR);
  187.    }
  188.  
  189.    else if (storage == 0)
  190.    {
  191.       (void)fprintf(stderr, "No storage was needed (???)\n");
  192.       return;
  193.    }
  194.  
  195.    /* -------------------------------------- */
  196.  
  197.    symtab = (asymbol **)malloc(storage);
  198.  
  199.    numSyms = bfd_canonicalize_symtab(abfd, symtab);
  200.    if (numSyms == ERROR)
  201.    {
  202.       bfderror("[bfd_canonicalize_symtab] error");
  203.       exit(ERROR);
  204.    }
  205.  
  206.    for (i = 0; i < numSyms; i++)
  207.    {
  208.       if (isalpha(symtab[i]->name[0]))
  209.       {
  210.          if (strcmp(symtab[i]->name, sym1) == 0)
  211.          {
  212.             foundsym1 = 1; 
  213.             sym1addr = symtab[i]->value;
  214.  
  215.             printf("-------------------------------------------------\n");
  216.  
  217.             printf("Symbol's name: %s\n", symtab[i]->name);
  218.             printf("Symbol's value: 0x%x (%d)\n",
  219.                    (u_int)symtab[i]->value, (u_int)symtab[i]->value);
  220.  
  221.             printf("Symbol's address: %p\n", symtab[i]->udata.p);
  222.             printf("Symbol's flags: "), fflush(stdout);
  223.  
  224.             if (symtab[i]->flags & BSF_LOCAL) (void)printf("LOCAL ");
  225.             else if (symtab[i]->flags & BSF_LOCAL) (void)printf("GLOBAL ");
  226.  
  227.             if (symtab[i]->flags & BSF_EXPORT) (void)printf("EXPORT ");
  228.  
  229.             if (symtab[i]->flags & BSF_FUNCTION) (void)printf("FUNCTION ");
  230.             if (symtab[i]->flags & BSF_WEAK) (void)printf("WEAK ");
  231.  
  232.             if (symtab[i]->flags & BSF_SECTION_SYM)
  233.                (void)printf("SECTION_SYM ");
  234.  
  235.             if (symtab[i]->flags & BSF_FILE) (void)printf("FILE ");
  236.             if (symtab[i]->flags & BSF_DYNAMIC) (void)printf("DYNAMIC ");
  237.             if (symtab[i]->flags & BSF_OBJECT) (void)printf("OBJECT ");
  238.  
  239.             putchar('\n');
  240.          }
  241.  
  242.          if (strcmp(symtab[i]->name, sym2) == 0)
  243.          {
  244.             foundsym2 = 1;
  245.             sym2addr = symtab[i]->value;
  246.  
  247.             printf("\n-------------------------------------------------\n");
  248.             (void)printf("Symbol's name: %s\n", symtab[i]->name);
  249.             (void)printf("Symbol's value: 0x%x (%d)\n",
  250.                          (u_int)symtab[i]->value, (u_int)symtab[i]->value);
  251.  
  252.             (void)printf("Symbol's address: %p\n", symtab[i]->udata.p);
  253.             (void)printf("Symbol's flags: "), (void)fflush(stdout);
  254.  
  255.             if (symtab[i]->flags & BSF_LOCAL) (void)printf("LOCAL ");
  256.             else if (symtab[i]->flags & BSF_LOCAL) (void)printf("GLOBAL ");
  257.  
  258.             if (symtab[i]->flags & BSF_EXPORT) (void)printf("EXPORT ");
  259.  
  260.             if (symtab[i]->flags & BSF_FUNCTION) (void)printf("FUNCTION ");
  261.             if (symtab[i]->flags & BSF_WEAK) (void)printf("WEAK ");
  262.  
  263.             if (symtab[i]->flags & BSF_SECTION_SYM)
  264.                (void)printf("SECTION_SYM ");
  265.  
  266.             if (symtab[i]->flags & BSF_FILE) (void)printf("FILE ");
  267.             if (symtab[i]->flags & BSF_DYNAMIC) (void)printf("DYNAMIC ");
  268.             if (symtab[i]->flags & BSF_OBJECT) (void)printf("OBJECT ");
  269.  
  270.             printf("\n\n");
  271.          }
  272.       }
  273.    }
  274.  
  275.    if ((sym1addr > sym2addr) && ((sym1addr > 0) && (sym2addr > 0)))
  276.    {
  277.       (void)fprintf(stderr, "error: symbol '%s' > symbol '%s' "
  278.                     "(can't overflow %s into %s)\n", 
  279.                     sym1, sym2, sym1, sym2);
  280.  
  281.       return;
  282.    }
  283.  
  284.    else if ((sym1addr == sym2addr) && (sym1addr > 0) && (sym2addr > 0))
  285.       (void)printf("%s's addr = %s's addr\n", sym1, sym2);
  286.  
  287.    else if ((sym1addr > 0) && (sym2addr > 0))
  288.       (void)printf("offset from %s to %s = 0x%x (%d) bytes\n", sym1, sym2,
  289.                    sym2 - sym1, sym2 - sym1);
  290.  
  291.    if (foundsym1 != 1) 
  292.       fprintf(stderr, "error: couldn't find symbol '%s'\n", sym1);
  293.  
  294.    if (foundsym2 != 1) 
  295.       fprintf(stderr, "error: couldn't find symbol '%s'\n", sym2);
  296.  
  297.    free(symtab);
  298. }
  299.  
  300. /* ------------------------------------- */
  301.  
  302. /* our error message handler */
  303. void bfderror(char *fmt, ...)
  304. {
  305.    va_list va;
  306.    char errbuf[512] = {0};
  307.  
  308.    va_start(va, fmt);
  309.    vsnprintf(errbuf, sizeof(errbuf)-1, fmt, va);
  310.    va_end(va);
  311.  
  312.    bfd_perror(errbuf);
  313. }
  314.  
  315.  
  316.